import { defineStore } from "pinia";
// import router from "@/router";
import { permissionRoutes } from "@/router/routes";
import { queryInfoList } from "@/api";
import { useSessionStorage } from "@/hook";

const [token, setToken, removeToken] = useSessionStorage("token");

export const useUserStore = defineStore("User", {
  state: () => ({
    token: token || "",
    menuRoutes: permissionRoutes,
    roles: [],
    userInfo: {},
  }),
  actions: {
    setToken(token) {
      this.token = token;
      setToken(token);
    },
    async setUserInfo() {
      const result = await queryInfoList();
      if (result.code == 200) {
        // TODO: 异步路由动态追加
        this.userInfo = result.data.checkUser;
        this.menuRoutes = [...permissionRoutes];
        return true;
      } else {
        return Promise.reject(new Error(result.message));
      }
    },
    userLogout() {
      this.token = "";
      this.userInfo = {};
      removeToken();
    },
  },
});
